﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class HocKyRepository:IHocKyRepository
    {
        DangKyPhuEntities entities;

        public HocKyRepository()
        {
            entities = new DangKyPhuEntities();
        }

        public IQueryable<int> LayHocKyID(int nienKhoaID)
        {
            return (from m in entities.HocKies
                        where m.NgayBatDau.Value.Year>=nienKhoaID
                        select m.HocKyID);
        }


        public HocKy LayHocKyHienTai()
        {
            return (from m in entities.HocKies
                        where m.LaHocKyHienHanh==true
                        select m).FirstOrDefault();
        }




        public IQueryable<HocKy> LayHocKyTheoTen(string tenNam)
        {
            return (from m in entities.HocKies
                        where m.TenHocKy.Contains(tenNam)
                        select m);
        }


        public IQueryable<HocKy> LayHocKyTheoNam(int nam)
        {
            return (from m in entities.HocKies
                        where m.NamHoc.Value==nam
                        select m);
        }



        #region IHocKyRepository Members


        public IQueryable<HocKy> LayHocKyTheoID(int id)
        {
            return from m in entities.HocKies
                   where m.HocKyID == id
                   select m;
        }

        #endregion

        #region IHocKyRepository Members

        /// <summary>
        /// lay hoc theo hoc ky so vd nhu hk3,hk4
        /// </summary>
        /// <param name="hockySo"></param>
        /// <returns></returns>
        public IQueryable<HocKy> LayHocKyTheoHocKy(int nienKhoaID, int hockySo)
        {
            int hocky=0, namhoc=0;
            if (hockySo % 2 == 0)
                hocky = 2;
            else
                hocky = 1;
            //nam hoc
            if (hockySo < 3)
                namhoc = nienKhoaID;
            else
            {
                hockySo -= 2;
                namhoc = nienKhoaID + (hockySo / 2) ;
            }
            return from m in entities.HocKies
                   where m.HocKy1.Value == hocky && m.NamHoc.Value == namhoc
                   select m;
        }

        #endregion
    }
}
